<?php 
/**
 * Controlador de el mantenimiento de ubicacion de documentos
 * @author Rudys Natanael
 * 
 */

class estatusEntrada extends Controller
{
	public function __construct()
	{
		parent::Controller();
	    $this->load->model(array('enestatus'));
	}
	
	public function index()
	{
		$data = $this->enestatus->loadDataEstatus();
		$this->load->view('main', array('content_page' => 'listEstatusEntrada','menutop'=>'2'));
	}
	
	public function data()
	{
			$page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
			$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'idEstatus'; // Columna para el order by
			$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
			$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
			$start = $limit * $page - $limit;
			if($start < 0) $start = 0;
			
		    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
			header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
			header("Cache-Control: no-cache, must-revalidate" );
			header("Pragma: no-cache" );
			
			$dataArray = $this->enestatus->loadDataEstatus($start, $limit, $sortname, $sortorder);			
			$totalDataArray = count($dataArray);
			
			$totalPages = ceil($totalDataArray/$limit);
			if ($page > $totalPages) $page = $totalPages;

			$json_output = array(
				'page'		=> $page,
				'total'		=> $totalPages,
				'records'	=> $totalDataArray,
				'rows'		=> array()
			);
			
		   if($dataArray != false) {
				foreach($dataArray as $k) {
					$arr = $json_output['rows'][] = array(
					   'idEtatusEntrada'   => $k->getIdEstatusEntrega(),
					   'descEtatusEntrada' => $k->getDescEstatusEntrega());
				}
			}
			
				
			echo json_encode($json_output);
	}
	
	public function add() 
	{
		$this->load->view('main', array('content_page' => 'addEstatusEntrada','menutop'=>'2'));	
	}
	
	public function edit($id)
	{
		 $objeto  = $this->enestatus->loadEstatuscId($id);
		 $this->load->view('main', array('content_page' => 'editEstatusEntrada','menutop'=>'2','data'=>$objeto));
	}
	
	public function delete()
	{
		$id = $this->input->post('delete_rows');
		$where = array('idEstatus'=>$id);
		
		 if ($this->enestatus->delEstatus($where) == true) {
			      $this->session->set_flashdata('error_title', 'Se a eliminado el estatus');
		 }else{
			 	$this->session->set_flashdata('error_title', 'hubo un error en la eliminacion');
		 }  
		  
	}
	
	public function save()
	{
		if($this->input->post('idestatus') === false){
			
			 $descUbicacion = $this->input->post('name');
			 $data = array(
			                'descEstatus'=>$descUbicacion
			              );

			 if ($this->enestatus->crearNuevoEstatus($data) == true) {
			      $this->session->set_flashdata('error_title', 'El nuevo estatus se creo con exitos');
			 }else{
			 	$this->session->set_flashdata('error_title', 'hubo un error en la creacion');
			 }              
		             
			redirect('estatusEntrada'); 
		}else{
			
			 $descUbicacion = $this->input->post('name');
			 $id =  $this->input->post('idestatus');
			 
			 $data = array('descEstatus'=>$descUbicacion);
			 $where = array('idEstatus'=>$id); 
			                           
			 if ($this->enestatus->upDateEstatus($data, $where) == true) {
			      $this->session->set_flashdata('error_title', 'el estatus se edito con exitos');
			 }else{
			 	$this->session->set_flashdata('error_title', 'hubo un error en la edicion');
			 }              
			redirect('estatusEntrada'); 
		}   
	}
	
}


?>